package com.lin.eat.config;

import com.lin.eat.entry.ImagePath;
import com.lin.eat.interceptor.TokenInterceptor;
import com.lin.eat.dao.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Autowired
    CustomerMapper customerMapper;

    @Bean
    public WebMvcConfigurer webMvcConfigurerAdapter(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index").setViewName("index");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //静态资源：*.css ，*.js
                //SpringBoot已经做好了静态资源映射
                registry.addInterceptor(new TokenInterceptor(customerMapper)).addPathPatterns("/**")
                        .excludePathPatterns("/","/customerLogin","/customerRegister"
                                ,"/customerLogout","/static/headImage/**","/static/goodImage/**");
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //关于图片上传后需要重启服务器才能刷新图片
                //这是一种保护机制，为了防止绝对路径被看出来，目录结构暴露
                //解决方法:将虚拟路径/static/headImage/
                //        向绝对路径 (E:\IDEA项目空间\测试\eat\src\main\resources\static\headImage\)映射

                //E:\IDEA项目空间\测试\eat\src\main\resources\static\headImage\
                String headPath = ImagePath.PROJECT_PATH + ImagePath.HEAD_PATH;
                registry.addResourceHandler("static/headImage/**").addResourceLocations("file:" + headPath);

                //E:\IDEA项目空间\测试\eat\src\main\resources\static\goodImage\
                String goodPath = ImagePath.PROJECT_PATH + ImagePath.GOOD_PATH;
                registry.addResourceHandler("static/goodImage/**").addResourceLocations("file:" + goodPath);

            }

        };
        return adapter;
    }

}
